package com.powernode.shop.oauth.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
* @author ZHAOZHENYU
* @since 2022/10/9
*/
/**
    * 系统用户
    */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sys_user")
public class SysUser implements UserDetails {
    @TableId(value = "user_id", type = IdType.AUTO)
    private Long userId;

    /**
     * 用户名
     */
    @TableField(value = "username")
    private String username;

    /**
     * 密码
     */
    @TableField(value = "password")
    private String password;

    /**
     * 邮箱
     */
    @TableField(value = "email")
    private String email;

    /**
     * 手机号
     */
    @TableField(value = "mobile")
    private String mobile;

    /**
     * 状态  0：禁用   1：正常
     */
    @TableField(value = "status")
    private Byte status;

    /**
     * 创建者ID
     */
    @TableField(value = "create_user_id")
    private Long createUserId;

    /**
     * 创建时间
     */
    @TableField(value = "create_time")
    private Date createTime;

    @TableField(exist = false)
    private List<String> auths;

    /**
     * 用户所在的商城Id
     */
    @TableField(value = "shop_id")
    private Long shopId;

    public static final String COL_USER_ID = "user_id";

    public static final String COL_USERNAME = "username";

    public static final String COL_PASSWORD = "password";

    public static final String COL_EMAIL = "email";

    public static final String COL_MOBILE = "mobile";

    public static final String COL_STATUS = "status";

    public static final String COL_CREATE_USER_ID = "create_user_id";

    public static final String COL_CREATE_TIME = "create_time";

    public static final String COL_SHOP_ID = "shop_id";

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 转换权限
        ArrayList<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
        // 处理权限  ，  sys:add,sys:del
        // @PreAuthority('sys:add')
        auths.forEach(auth -> {
            // 取出的数据库 有多个权限字符串，按照逗号切出来变成字符串数组，在变成权限对象，便于后面操作请求方法 来追加权限
            if (auth.contains(",")) {
                String[] realAuths = auth.split(",");
                for (String realAuth : realAuths) {
                    simpleGrantedAuthorities.add(new SimpleGrantedAuthority(realAuth));
                }
            } else {
                simpleGrantedAuthorities.add(new SimpleGrantedAuthority(auth));
            }
        });
//        System.out.println(simpleGrantedAuthorities);
        return simpleGrantedAuthorities;

    }

    @Override
    public String getPassword(){
        return this.password;
    }

    /**
     * 将用户id暴露到Authentication的Principal里面去，不能暴露username，不好操作数据库
     * @return
     */
    @Override
    public String getUsername(){
        return String.valueOf(this.userId);
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status==1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status==1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status==1;
    }

    @Override
    public boolean isEnabled() {
        return this.status==1;
    }
}